package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.accumulated;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.QuotaConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.medium.PolicyRiskInfoModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.riskamount.CalculateRiskAmount;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * description:
 *
 * @author jackdaw
 * @date 2020-06-24 16:41
 */
public class Hct010AccumulatedStrategy implements AccumulatedStrategy {
    @Override
    public InsuredModel checkAccumulatedRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository, ProductModel productModel, RedisService redisService, ProductConfig productConfig) {
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(productModel.getProdInsSeq(), requestModel.getCustInfo().getInsurants().getInsurant());
        Set riskCodeSet = null;
        try {
            riskCodeSet = redisService.members(AcceptConstant.SERIOUS_ILLNESS_INSURANCE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (CollectionUtils.isEmpty(riskCodeSet)) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.EXCEPTION_SERIOUS_ILLNESS, insurantModel.getName()));
        }
        List<ProductModel> productModelList = requestModel.getProductInfo().getProducts().getProduct();
        List<PolicyRiskInfoModel> policyRiskInfoList = new ArrayList<>(productModelList.size());
        for (ProductModel product : productModelList) {
            if (insurantModel.getInsSeq() != product.getProdInsSeq()) {
                continue;
            }
            if (!redisService.exemptionProduct(product.getProdCode())) {
                policyRiskInfoList.add(CalculateRiskAmount.getPolicyRiskInfoModel(product, requestModel));
            }
        }
        BigDecimal illnessNow = CalculateRiskAmount.getSumInsuranceRiskAmount(riskCodeSet, policyRiskInfoList, true);
        BigDecimal illnessCache = redisService.getCacheQuota(QuotaConstant.SORT_A, insurantModel.getIdNo(), illnessNow, requestModel.getPolicyInfo().getContNo());
        double illnessHistory = 0.0;
        if (StringUtils.isNotBlank(insurantModel.getInsuredNo()) && !StringUtils.equals(AcceptConstant.MINUS_ONE, insurantModel.getInsuredNo())) {
            illnessHistory = insuredCoreRepository.getSeriousDiseaseInsuranceCount(insurantModel.getInsuredNo(), insurantModel.getInsuredNo());
        }
        BigDecimal total = illnessNow.add(BigDecimal.valueOf(illnessHistory)).add(illnessCache);
        Date valiDate = requestModel.getPolicyInfo().getCvalidate();
        //获取日期相差的天数
        int days = DateUtil.getDays(insurantModel.getBirthday(), valiDate);
        int years = DateUtil.getYears(insurantModel.getBirthday(), valiDate);
        if (days >= UnderWriteConstant.AGE_THIRTY_DAY && years <= UnderWriteConstant.AGE_FORTY_YEAR && total.compareTo(BigDecimal.valueOf(UnderWriteConstant.FOUR_HUNDRED_THOUSAND)) > 0) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_RISK_MESSAGE, insurantModel.getName(), years, UnderWriteConstant.STRING_FOUR_HUNDRED_THOUSAND));
        } else if (years >= UnderWriteConstant.AGE_FORTY_ONE_YEAR && years <= UnderWriteConstant.AGE_FORTY_FIVE_YEAR && total.compareTo(BigDecimal.valueOf(UnderWriteConstant.THREE_HUNDRED_THOUSAND)) > 0) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_RISK_MESSAGE, insurantModel.getName(), years, UnderWriteConstant.STRING_THREE_HUNDRED_THOUSAND));
        } else if (years >= UnderWriteConstant.AGE_FORTY_SIX_YEAR && years <= UnderWriteConstant.AGE_FIFTY_YEAR && total.compareTo(BigDecimal.valueOf(UnderWriteConstant.TWO_HUNDRED_THOUSAND)) > 0) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    String.format(UnderWriteConstant.UNDER_WRITE_RISK_MESSAGE, insurantModel.getName(), years, UnderWriteConstant.STRING_TWO_HUNDRED_THOUSAND));
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }
}
